var express = require('express');
var http = require("http");
var qs = require('querystring');

var router = express.Router();

//server
//var host = "silverybullet.me"

//test
var host = "127.0.0.1"
var port = 8080;

//用户登录页面
router.get('/login', function(req, res){
	res.status(200).render('user/login.ejs');
})

router.post('/doLogin', function(req, res){
	var userName = req.body.userName;
	var passWord = req.body.passWord;
	var rememberMe = req.body.rememberMe;

	//res.send(req.body)
	//res.send(userName + ":" + passWord + ":" + rememberMe)
	//res.end();

	/*
	var options = {
	    type : "POST", 
	    url  : baseUrl + "/user/login.action", 
	    async : false, 		//同步请求数据
	    dataType : 'json',
	    data : {
	    	loginName : userName,
	    	password : passWord,
	    	rememberMe : rememberMe
	    },
	    contentType : "text/plain",
	    success : function(data) {
	    	//检查code是不是200
	    	if (data && data.code == 200){
	    		res.render('home.ejs');
	    	}
	    	else if(data && data.code == 401){
	    		res.render('user/login.ejs');
	    	}
	    	else{
	    		res.render('user/login.ejs');
	    	}
	    },
	    error : function(){
	    	console.log('login error');
	    }
	}

	$.ajax(options);
	*/

	var opt = {
		method : "POST",
		host : host,
		port : port,
		path : "/das/user/login.action"
	}

	var req = http.request(opt, function(server){
		console.log(server.statusCode)
		if (server.statusCode != 200){
			var goal = "home.ejs";
			server.on('data', function(data){
				console.log(data);
				if (data && data.code == 200){
		    		
		    	}
		    	else if(data && data.code == 401){
		    		goal = 'user/login.ejs';
		    	}
		    	else{
		    		goal = 'user/login.ejs';
		    	}
			})
			.on('end', function(){
				res.render(goal);
			});
		}
		else{
			res.send(500, "error");
		}
	});

	req.on('error', function(e){
		console.log('服务器异常: ' + e.message);
	});

	req.end();
})

//用户注册页面
router.get('/register', function(req, res){
	res.status(200).render('user/register.ejs');
})

router.post('/doRegister', function(req, res){
	var loginName = req.body.loginName;
	var userName = req.body.userName;
	var passWord = req.body.passWord;
	var email = req.body.email;

	//res.send(req.body)

	/*
	var options = {
	    type : "POST", 
	    url  : baseUrl + "/user/register.action", 
	    timeout : 5000, //超时时间设置，单位毫秒
	    async : false, 		//同步请求数据
	    dataType : 'json',
	    data : {
	    	loginName : loginName,
	    	userName : userName,
	    	password : passWord,
	    	email : email
	    },
	    contentType : "text/plain",
	    success : function(data) {
	    	res.render('home.ejs')
	    	//检查code是不是200
	    	//登陆成功
	    	//检查==401
	    	//用户名密码错误
	    	//服务器异常
	    },
	    error : function(){
	    	console.log('login error');
	    	res.render('home.ejs')
	    },
	    complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
	　　　　if(status=='timeout'){//超时,status还有success,error等值的情况
	 　　　　　 ajaxTimeoutTest.abort();
	　　　　　  alert("超时");
	　　　　}
	　　}
	}
	*/
})

//用户注册页面
router.get('/test', function(req, res){
	res.status(200).render('home.ejs');
})

/*
router.get('/:id', function(req, res){
	console.log(req.params.id);
	res.end()
})

router.get('/ft/ft', function(req, res){
	res.status(403).send('forbidden');
	res.end();
})

router.get('/tt/tt', function(req, res){
	res.contentType('application/javascript');
	res.sendFile('/ok.js', {root : __dirname + '/../public'});
	res.end();
})

router.get('/mm/mm', function(req, res){
	res.json({a:1, b : [true, 'ok']});
	res.end();
})

router.all('/list', function(req, res){
	console.log(req.method);
	console.log(req.baseUrl);
	console.log(req.path);
	console.log(req.headers['user-agent']);
	console.log(req.get('user-agent'));

	console.log(req.query);
	console.log(req.query.id);

	console.log(req.body);
	console.log(req.body.id);

	res.send('route list');
});
*/

module.exports = router;